package com.bluefox.modules.system.service.impl;

import com.bluefox.utils.PageUtil;
import com.bluefox.utils.QueryHelp;
import com.bluefox.utils.UpdateUtil;
import com.bluefox.modules.system.criteria.DictDataCriteria;
import com.bluefox.modules.system.domain.SysDictData;
import com.bluefox.modules.system.repository.SysDictDataRepository;
import com.bluefox.modules.system.service.SysDictDataService;
import com.bluefox.modules.system.service.dto.SysDictDataDto;
import com.bluefox.modules.system.service.mapper.SysDictDataMapper;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;

import javax.annotation.Resource;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

/**
 * (SysDictData)表服务实现类
 *
 * @author makejava
 * @since 2020-05-21 15:24:19
 */
@Service("sysDictDataService")
public class SysDictDataServiceImpl implements SysDictDataService {
    @Resource
    private SysDictDataRepository sysDictDataRepository;

    @Resource
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public SysDictDataDto findById(Long dictId) {

        return sysDictDataMapper.toDto(sysDictDataRepository.findById(dictId).get());
    }

    @Override
    public List<SysDictDataDto> getAll() {


        return sysDictDataMapper.toDto(sysDictDataRepository.findAll());

    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return List<SysDictDataDto>
     */
    @Override
    public List<SysDictDataDto> selectDictDataByType(String dictType) {

        return sysDictDataMapper.toDto(sysDictDataRepository.findByDictType(dictType));

    }

    @Override
    public Object getAll(DictDataCriteria criteria, Pageable pageable) {
        Page<SysDictData> page = sysDictDataRepository
                .findAll((Specification<SysDictData>) (root, query, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(sysDictDataMapper::toDto));
    }


    @Override
    @Transactional
    public SysDictDataDto insert(SysDictDataDto sysDictDataDto) {
        SysDictData sysDictData = new SysDictData();

        UpdateUtil.copyNullProperties(sysDictDataDto, sysDictData);
        SysDictData save = this.sysDictDataRepository.save(sysDictData);
        return sysDictDataMapper.toDto(save);
    }


    @Override
    @Transactional
    public SysDictDataDto update(SysDictDataDto sysDictDataDto) {
        SysDictData sysDictData = sysDictDataRepository.findById(sysDictDataDto.getId()).orElseGet(SysDictData::new);
        UpdateUtil.copyNullProperties(sysDictDataDto, sysDictData);
        SysDictData save = this.sysDictDataRepository.save(sysDictData);
        return sysDictDataMapper.toDto(save);
    }


    @Override
    @Transactional
    public boolean deleteByIds(Long[] ids) {

        try {
            for (Long id : ids) {
                sysDictDataRepository.deleteById(id);
            }
        } catch (Exception ex) {
            return false;
        }
        return true;

    }
}
